package com.navinfo.platform.aero.data.rest.core.repository;

import com.navinfo.platform.aero.data.rest.core.entity.ColumnsMapper;
import com.navinfo.platform.aero.data.rest.core.mapper.ColumnsMapperMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
@CacheConfig(cacheNames = "columns")
public class QueryRepository {

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;
    @Autowired
    private ColumnsMapperMapper columnsMapper;

    private final Logger LOG = LoggerFactory.getLogger(getClass());
    @Cacheable
    public List<ColumnsMapper> queryColumnsList(List<String> columns, String parent, List<String> childs, String cycle) throws Exception {
        try {
            return columnsMapper.getColumns(columns, parent, childs, cycle);
        } catch(Exception e){
            LOG.error("查询数据库失败", e);
            throw new Exception("查询数据库失败");
        }
    }
    @Cacheable
    public List<ColumnsMapper> queryColumnsList(List<String> parents, String cycle) throws Exception {
        try {
            return columnsMapper.getColumnsByParent(parents, cycle);
        } catch(Exception e){
            LOG.error("查询数据库失败", e);
            throw new Exception("查询数据库失败");
        }
    }
    public List<Map<String,Object>> queryList(String sql, Long uid, int start, int end) throws Exception {

        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("uid",uid);
        paramMap.put("start",start);
        paramMap.put("end",end);
        try {
            return jdbcTemplate.queryForList(sql,paramMap);
        } catch(Exception e){
            LOG.error("查询数据库失败", e);
            throw new Exception("查询数据库失败");
        }
    }
}